#include "dyn_roots_multiDim_newtonRaphson.h"

MultiDimNewtonRaphson::MultiDimNewtonRaphson(MultiFunctor &func, MultiFunctor &jac, vec init, const double tol, const int Nmax)
    :m_function(func), m_jacobian(jac)
{
    setInitialValue(init);
    setTolerance(tol);
    setMaxIterations(Nmax);
}

int MultiDimNewtonRaphson::solve()
{
    // We start the clock
    start = clock();

    // We initialize the return code.
    // A priori, things go bad.
    int returnCode(1);

    // We set m_root to the initialValue.
    // The algorithm will update this value directly.
    m_root = m_initialValue;

    // Actual iterative algorithm.
    for (int i=0;i<maxIterations;i++)
    {
        vec dx = arma::solve(m_jacobian(m_root), -m_function(m_root));
        m_root += dx;

        iterations++;

        if (norm(dx,1) < tolerance)
        {
            returnCode = 0;
            break;
        }
    }
    return returnCode;
}
